//
//  ChatViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//



#import "ChatViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "ChatSetUpViewController.h"
#import "GroupSetUpViewController.h"
#import <TUIInputBar.h>
@interface ChatViewController ()<TUIChatControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIDocumentPickerDelegate>
/**
 chat
 */
@property (nonatomic , strong) TUIChatController *chat;
@end

@implementation ChatViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
    [self setUpNav];
}


#pragma mark - init method
- (void)setUpNav{
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"discountcoupon_more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(moreClick)];
    
    [self.baseNavView.rightNavBtn setImage:[UIImage imageNamed:@"discountcoupon_more"] forState:UIControlStateNormal];
    self.baseNavView.rightNavBtn.hidden = NO;
    
    @weakify(self);
    self.baseNavView.rightBlock = ^{
        @strongify(self);
        [self moreClick];
    };
}


- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    
    TIMConversation *conv = [[TIMManager sharedInstance] getConversation:self.conversation.convType receiver:self.conversation.convId];
    
    self.chat = [[TUIChatController alloc] initWithConversation:conv];
    self.chat.delegate = self;
    [self addChildViewController:self.chat];
    self.chat.view.mj_y = TJ_NavBarHeight;
    self.chat.view.height = KScreenHeight - TJ_NavBarHeight;
    [self.view addSubview:self.chat.view];
    self.baseNavView.navTitleLabel.text = self.conversation.title;
}

- (void) initLayout {
    
}

- (void) initAction {
    UIButton *recordButton = self.chat.inputController.inputBar.recordButton;
    [recordButton addTarget:self action:@selector(recordButtonClick:) forControlEvents:UIControlEventTouchDown];
}

- (void)initData {
    
}

#pragma mark - delegate
//发送相册 拍照 视频  文件 权限校验
- (void)chatController:(TUIChatController *)chatController onSelectMoreCell:(TUIInputMoreCell *)cell {
    if (cell.data == [TUIInputMoreCellData photoData]) {
        if ([[AuthorizationManager sharedManager] camerAuthorizationAllowed]) {}
    }
    if (cell.data == [TUIInputMoreCellData videoData]) {
        if ([[AuthorizationManager sharedManager] microphoneAuthorizationAllowed]) {}

    }
    if (cell.data == [TUIInputMoreCellData pictureData]) {
        if ([[AuthorizationManager sharedManager] photoAlbumAuthorizationAllowed]) {}
    }
}


#pragma mark - target action

- (void)moreClick {
    
//    switch (self.conversation.convType) {
//
//        case TIM_C2C:
//        {
//             [[TIMFriendshipManager sharedInstance] getFriendList:^(NSArray<TIMFriend *> *friends) {
//                for (TIMFriend *friend in friends) {
//                    if ([friend.identifier isEqualToString:self.conversation.convId]) {
//                        ChatSetUpViewController *vc = [[ChatSetUpViewController alloc] init];
//                        vc.friendProfile = friend;
//                        [self.navigationController pushViewController:vc animated:YES];
//                    }
//                }
//            } fail:^(int code, NSString *msg) {
//
//                NSLog(@"___________-   %@", msg);
//                [self showHudWithMsg:msg];
//            }];
//         }
//            break;
//
//
//        case TIM_GROUP:
//        {
//            GroupSetUpViewController *vc = [[GroupSetUpViewController alloc] init];
//            vc.groupId = self.conversation.convId;
//            vc.groupName = self.conversation.title;
//            [self.navigationController pushViewController:vc animated:YES];
//
//        }
//            break;
//
//        default:
//        {
//
//        }
//            break;
//    }
    

    switch (self.conversation.convType) {
        case TIM_C2C:
            {
                [self showHudWithMsg:@"会话"];
                [[TIMFriendshipManager sharedInstance] getFriendList:^(NSArray<TIMFriend *> *friends) {
                    for (TIMFriend *friend in friends) {
                        if ([friend.identifier isEqualToString:self.conversation.convId]) {
                            ChatSetUpViewController *vc = [[ChatSetUpViewController alloc] init];
                            vc.friendProfile = friend;
                            [self.navigationController pushViewController:vc animated:YES];
                        }
                    }
                } fail:^(int code, NSString *msg) {
                    
                    NSLog(@"___________-   %@", msg);
                    [self showHudWithMsg:msg];
                }];
            }
            break;
        case TIM_GROUP:
            {
                [self showHudWithMsg:@"组聊"];

            }
            break;
        
        default:
            {
            [self showHudWithMsg:@"系统"];

            }
            break;
    }
    
}


- (void)recordButtonClick:(UIButton *)button {
    //查看是否开启麦克风权限
    if ([[AuthorizationManager sharedManager] microphoneAuthorizationAllowed]) {
        
    }
}
#pragma mark - private method

#pragma mark - getter and setter




@end
